1102E - Monotonic Renumeration - CodeForces Solution


combinatorics sortings *1700

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.buffer.readline 

p = 998244353 

def process(A):
    n = len(A)
    A2 = []
    for i in range(n):
        A2.append([A[i], i])
    A2.sort()
    L = []
    curr = [A2[0][0], A2[0][1], A2[0][1]]
    for i in range(1, n):
        if A2[i][0] > curr[0]:
            L.append(curr)
            curr = [A2[i][0], A2[i][1], A2[i][1]]
        curr[2] = A2[i][1]
    L.append(curr)
    L.sort(key=lambda a: a[1])
    blocks = 0
    block_end = L[0][2]
    for i in range(1, len(L)):
        x, start, end = L[i]
        if start > block_end:
            blocks+=1
            block_end = end 
        else:
            block_end = max(block_end, end)
    blocks+=1
    print(pow(2, blocks-1, p))
            
       
n = int(input())
A = [int(x) for x in input().split()]
process(A)

C++ Code:

#include<bits/stdc++.h>
#define fast_cin() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
// long long int n,m,k,flag=0,sz=0,v[51][51],dx[]={0,0,0,1},dtm[]={1,0,0,0};
// string s[51];
// vector<pair<long long int,pair<long long int,long long int>>>vv;
// void dfschng(long long int x,long long int y)
// {
//  if(x<0 || y<0 || x>=n || y>=m)
//  {
//   return;
//  }
//  if(v[x][y] || s[x][y]=='*')
//  {
//   return;
//  }
//  //cout<<x<<" "<<y<<"\n";
//  v[x][y]=1;s[x][y]='*';
//  for(long long int i=0;i<4;i++)
//  {
//   long long int xx=x+dx[i],yy=y+dtm[i];
//   dfschng(xx,yy);
//  }
// }
// void dfs(long long int x,long long int y)
// {
//  if(x<0 || y<0 || x>=n || y>=m)
//  {
//   return;
//  }
//  if(v[x][y] || s[x][y]=='*')
//  {
//   return;
//  }
//  v[x][y]=1;sz++;
//  if(x==0 || y==0 || x==(n0) || y==(m0))
//  {
//   flag=1;
//  }
//  for(long long int i=0;i<4;i++)
//  {
//   long long int xx=x+dx[i],yy=y+dtm[i];
//   dfs(xx,yy);
//  }
// }
// long long int n;
//  cin>>n;
//  for(long long int i=1;i<=n;i+=2)
//  {
//   cout<<i<<" ";
//  }
//  for(long long int i=n-(n&1);i>=1;i-=2)
//  {
//   cout<<i<<" ";
//  }
//  cout<<"\n";
void solve()
{
 long long int n,x=-1,mod=998244353,op=1;
 cin>>n;
 vector<long long int>v(n,0),pos(n,0);
 map<long long int,long long int>mp;
 for(auto&vv:v)
 {
  cin>>vv;
 }
 for(long long int i=n-1;i>=0;i--)
 {
  if(!mp.count(v[i]))
  {
   mp[v[i]]=i;
  }
  pos[i]=mp[v[i]];
 }
 for(long long int i=0;i<n-1;i++)
 {
  x=max(x,pos[i]);
  if(x==i)
  {
    op=(2*op)%mod;
  }
 }
 cout<<op;
}
int main()
{
    fast_cin();
    long long int t;
    //cin>>t;
    t=1;
    while(t--)
    {
     solve();
    }
   return 0;
}


Comments

Submit
0 Comments
More Questions

1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings